const _ = require('lodash');
const moment = require('moment');
module.exports = app => {
    const {router, middleware} = app;

    /**公众日志*/
    router.get('/vpr/:index', async ctx => {
        const {index} = ctx.params;
        if (_.isEmpty(index))
            return ctx.helper.error('缺少必要参数');
        let data = await ctx.app.spiderReportData.get({index});
        await ctx.render('vpr/vpr.js', {data});
    });

    /**日志所有截图*/
    router.get('/vpic/:index', async ctx => {
        const {index} = ctx.params;
        let report = await ctx.app.model.Report.findOne({
            where: {index},
            include: [{model: ctx.app.model.Project}]
        });
        if (!report)
            return ctx.helper.error('不存在的日志');
        report = report.toJSON();
        let project = report.project;
        let {or, between} = ctx.app.Sequelize.Op;
        let start = moment(report.created_at).startOf('day').toDate(),
            end = moment(report.created_at).endOf('day').toDate();
        delete report.project;
        const schedules = await ctx.app.model.DetailAnchorSchedule.findAll({
            where: {
                project_id: project.id, [or]: {
                    start: {[between]: [start, end]},
                    end: {[between]: [start, end]}
                }
            },
            include: [
                {
                    model: ctx.app.model.DetailAnchor,
                    as: 'anchor',
                },
                {
                    model: ctx.app.model.File,
                    as: 'images',
                    where: {selected: true},
                    required: false,
                },
                {model: ctx.app.model.File, as: 'image', required: false,},
                {model: ctx.app.model.Platform},
            ]
        });
        await ctx.render('vpic/vpic.js', {report, project, schedules});
    });

    /**首页，与各子页面*/
    let index = async ctx => {
        if (ctx.user) {
            await ctx.render('app/app.js',
                {url: ctx.url, user: ctx.user, pageSize: ctx.app.pageSize}
            );
        }
        else {
            await ctx.render('login/login.js', {agreement: await ctx.app.model.Config.get('userAgreement')});
        }
    };
    router.get('/', index);
    router.get('/(.+)', index);
};